<template>
  <div>
    <div class="data">
      <div>
        <el-form :inline="true" :model="params" class="demo-form-inline">
          <el-form-item label="名称">
            <el-input
              v-model="params.name"
              placeholder="按群组名称查找"
              class="block"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              @click="searchList"
              class="block"
              >查询</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-plus"
              @click="$router.push('/index/groupEdit')"
              >增加</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="message">
      <div class="message1">
        <el-table :data="rows" stripe style="width: 100%">
          <el-table-column prop="name" label="名称" width="180">
          </el-table-column>
          <el-table-column prop="desoptions" label="描述">
          </el-table-column>
          <el-table-column prop="status" label="是否启用" width="180">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="1"
                inactive-value="2"
                @change="changeStatus(scope.row)">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="
                  $router.push({path:'/index/groupEdit',query:{_id:scope.row._id}})
                "
                >编辑</el-button>
                <!-- 打开分配菜单弹框 传入groupId -->
                <el-button
                  type="danger"
                  size="mini"
                  icon="el-icon-document"
                  @click="openMenus(scope.row._id)"
                  >分配菜单</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="pagination">
      <el-pagination
        @size-change="sizeChange"
        @current-change="currentChange"
        :current-page="params.pageNum"
        :page-sizes="[10, 100, 200, 300, 400]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog title="分配菜单" :visible.sync="visible">
      <menus ref="menu"></menus>
    </el-dialog>
  </div>
</template>

<script>
import menus from './menu'
export default {
  name: "groupList",
  components: {
    menus
  },
  data() {
    return {
      loading: false,
      params: {
        pageSize: 10,
        pageNum: 1,
        name:''
      },
      total: 0,
      rows: [],
      visible:false
    }
  },
  methods: {
    openMenus(groupId){
      //打开弹框
      this.visible = true
      //延迟执行
      this.$nextTick(() => {
        //调用菜单组件的获得数据方法
        this.$refs.menu.getList(groupId)
      })
    },
    changeStatus(row){
      this.$http({
        url:'/v1/groups',
        method:'PUT',
        data:row
      }).then(res => {
        console.log(res)
      })
    },
    sizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.params.pageNum = 1;
      this.getList();
    },
    currentChange(pageNum) {
      this.params.pageNum = pageNum;
      this.getList();
    },
    searchList() {
      this.params.pageNum = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      this.$http({
        url: "/v1/group",
        method: "GET",
        params: this.params,
      }).then((res) => {
        this.loading = false;
        this.total = res.total;
        this.rows = res.rows;
      });
    },
  },
  created() {
    this.getList();
  },
  mounted() {},
};
</script>

<style scoped>

</style>
